package com.crystal.jcms.shpz;

import com.crystal.constant.Constant;
import com.crystal.jcms.utils.ShowMsgUtils;
import com.crystal.jcms.utils.SpUtils;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class ActivitySetting extends Activity implements OnClickListener {
	private EditText txtServerUrl;
	private Button btnSave;
	private Context mContext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		ActionBar actionBar = getActionBar();
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setTitle("设置");
		setContentView(R.layout.activity_set);
		initWidget();
		initData();  
	}

	private void initWidget() {
		txtServerUrl = (EditText) findViewById(R.id.txtServerUrl);
		btnSave = (Button) findViewById(R.id.btnSave);
		btnSave.setOnClickListener(this);
	}
	
	private void initData(){
		if(SpUtils.contains(mContext, "ServerUrl")){
			txtServerUrl.setText(SpUtils.get(mContext, "ServerUrl", "").toString());
		}else{
			txtServerUrl.setText("http://221.2.55.194:85");
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnSave:
			if (txtServerUrl.getText().toString().equals("")) {
				Message msg = new Message();
				msg.what = Constant.SHOWTOAST;
				msg.obj = "请输入服务器地址！";
				handler.sendMessage(msg);
				return;
			}
			SpUtils.put(mContext, "ServerUrl", txtServerUrl.getText().toString());
			Intent intent = new Intent(ActivitySetting.this, ActivityLogin.class);
			startActivity(intent);
			ActivitySetting.this.finish();
			break;
		}
	}

	@SuppressLint("HandlerLeak")
	public Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case Constant.SHOWTOAST:
				ShowMsgUtils.ShowToast(mContext, (String) msg.obj);
			}
		}
	};
}
